package com.forezp.thinking.chapter18.nio;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileLock;
import java.util.concurrent.TimeUnit;

/**
 * Created by forezp on 2017/12/11.
 */
public class FileLocking {

    public static void main(String[] args) throws IOException, InterruptedException {
        FileOutputStream fos=new FileOutputStream(new File("java.txt"));
        FileLock lock=fos.getChannel().tryLock();
        if(lock!=null){
            System.out.println("file locked");
            TimeUnit.MILLISECONDS.sleep(1111);
            lock.release();
            System.out.println("file unlocked");
        }

    }
}
